<?php
	session_start();
	set_time_limit(0);
	include_once('init.php');
	try {
	$m  = new Mongo();
	} catch(Exception $e) {
	  echo "连接数据库失败:".$e->getMessage();	
	}
	$db = $m->selectDB('foobar');
	$collection = $db->selectCollection("test3");
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>PHP 操作 mongodb 数据库 by 此题无解</title>
		<meta charset="UTF-8">
		<meta name=description content="">
		<meta name=viewport content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- Bootstrap CSS -->
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	</head>
	<body>
	<h1 class="text-center">PHP 操作 mongodb 数据库(查询数组)</h1>
	<div class="container">
		<div class="row">
		1. 插入测试数据
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'inserttest4.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$data = array('fruit'=>array('apple','banana','peach'));
			$data1 = array('fruit'=>array('apple','kumquat','orange'));
			$data2 = array('fruit'=>array('cherry','banana','apple'));
			$collection->remove();
			$collection->insert($data);
			$collection->insert($data1);
			$collection->insert($data2);
			$cursor = $collection->find();
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(1);?></pre>
		</div>

		2. 查询有香蕉的文档
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find8.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('fruit'=>'banana'),
				array('_id'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(2);?></pre>
		</div>

		3. $all 查询数组同时包含apple,banana的元素,
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find9.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('fruit'=>
						array('$all'=>
							array('apple','banana')
							)
					),
				array('_id'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>

		4. 查询第3个元素是apple的文档
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find10.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('fruit.2'=>'apple'),
				array('_id'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(4);?></pre>
		</div>
		
		5. 查询数组长度为3的文档
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find11.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('fruit'=>
					array('$size'=>3)
					),
				array('_id'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(5);?></pre>
		</div>

		6. $slice 类似在shell下执行 skip(10).limit(10),跳过10条记录，显示10条<br>
		未用数据测试，执行正确
		<pre class="prettyprint linenums Lang-php">
		runtime(0);
			$result= array();
			$cursor = $collection->find(
				array(),
				array('comment'=>
					array(
						'$silice'=>array(10,10)
						)
					)
				);
		</pre>



		<div><blockquote><em>php</em>操作<em>mongodb</em>数据库入门篇(5)<br>
		<a href="sixth.php">下一篇</a>
		<small class="pull-right"><cite>此题无解</cite></small></blockquote></div>

		</div>
	</div>

		<!-- jQuery -->
		<script src="./js/jquery.min.js"></script>
		<!-- Bootstrap JavaScript -->
		<script src="./js/bootstrap.min.js"></script>
		<!-- prettify -->
		<script src="./js/prettify.js"></script>
		<script>
		$(function(){
			prettyPrint();
		});
		</script>
	</body>
</html>